import requests
from lxml import etree
import re

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
}


def weather_day30(text):
    tree = etree.HTML(text)
    word = tree.xpath('/html/body/div[3]/div[3]/div/div[1]/div/p/text()')[0]

    day30 = re.findall('window.forecast30Day = (.*?);', text)[0]
    data = re.findall('"date":"(.*?)","day":"(.*?)","code":"(.*?)","text":"(.*?)",'
                      '"maxTmp":"(.*?)","minTmp":"(.*?)","windDir":"(.*?)","windSc":"(.*?)",'
                      '"windSpeed":"(.*?)","hum":"(.*?)","uv":"(.*?)","pcpn":"(.*?)","sunRise":"(.*?)",'
                      '"sunSet":"(.*?)","max":(.*?),"min":(.*?)}', day30)
    day30_list = []
    for i in range(len(data)):
        obj = {
            'date': data[i][0],  # 日期
            'day': data[i][1],  # 哪天
            'code': data[i][2],  # 图标序号
            'text': data[i][3],  # 天气状态
            'maxTmp': data[i][4],  # 最高温
            'minTmp': data[i][5],  # 最低温
            'windDir': data[i][6],  # 吹什么风 （北风）
            'windSc': data[i][7],  # 风级
            'windSpeed': data[i][8],  # 风速
            'hum': data[i][9],  # 相对湿度
            'uv': data[i][10],  # 紫外线
            'pcpn': data[i][11],  # 降水量
            'sunRise': data[i][12],  # 日出时间
            'sunSet': data[i][13],  # 日落时间
            'max': data[i][14],  # 是否是这个月中的最高温
            'min': data[i][15],  # 是否是这个月中的最低温
        }
        day30_list.append(obj)
    # print(day30_list)
    data = {
        'word': word,
        'day30': day30_list
    }
    return data


def weather30Html(city, code):
    weather_url = f'https://www.qweather.com/weather30d/{city}-{str(code)}.html'
    try:
        response = requests.get(url=weather_url, headers=headers)

        data = weather_day30(response.text)
        return data
    except:
        string = '爬取失敗,请检查输入的参数是否正确'
        print(string)
        return string


# 测试代码
# weather30Html('guangzhou', 101280101)